package com.hksj.cbm.service;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hksj.cbm.dto.SpecialityModelDTO;
import com.hksj.cbm.entity.SpecialityModel;
import com.hksj.cbm.mapper.SpecialityModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zengke
 * @version 1.0
 * @see SpecialityModel
 * @since 2024/7/11 14:57
 */

@Service
public class SpecialityModelService extends ServiceImpl<SpecialityModelMapper, SpecialityModel> {

    /**
     * 新增专业模型
     */
    @Transactional
    public String createSpecialityModel(SpecialityModelDTO specialityModelDTO){

        //获取当前专业模型的code存在数量
        Integer existCount = lambdaQuery().eq(SpecialityModel::getCode, specialityModelDTO.getCode())
                .count();

        //判断专业模型已存在的数量是否大于0
        if (existCount > 0){
            throw new RuntimeException("该专业模型code已存在,请重新输入");
        }

        //获取当前节点的父节点路径
        String paths = specialityModelDTO.getPaths();

        //节点路径不为空
        Assert.hasText(paths,"节点不为空");

        //将SpecialityModelDTO类型转为SpecialityModel类型
        SpecialityModel specialityModel = Convert.convert(SpecialityModel.class, specialityModelDTO);

        //新增专业模型
        super.save(specialityModel);

        //当前节点的路径 为 "父节点路径" + "当前节点id"
        String currentPaths = paths + "," +specialityModel.getId();

        //设置当前路径
        specialityModel.setPaths(currentPaths);

        //更新专业模型
        super.updateById(specialityModel);

        return "新增成功";
    }

    /**
     * 更新专业模型
     */
    @Transactional
    public String updateSpecialityModel(SpecialityModelDTO specialityModelDTO){

        //将SpecialityModelDTO类型转为SpecialityModel类型
        SpecialityModel specialityModel = Convert.convert(SpecialityModel.class, specialityModelDTO);

        //更新专业模型
        super.updateById(specialityModel);

        return "更新成功";
    }

    /**
     * 删除专业模型
     */
    @Transactional
    public String deleteSpecialityModel(Long id){

        //查询专业模型
        SpecialityModel specialityModel = super.getById(id);

        //判断专业模型是否存在
        if (ObjectUtils.isEmpty(specialityModel)){
            throw new RuntimeException("专业模型不存在,请刷新后重试");
        }

        //删除专业模型
        super.removeById(id);

        return "删除成功";
    }

    /**
     * 查询专业模型树
     */
    public List<SpecialityModel> querySpecialityModelTree(){
        //查询所有的专业模型
        List<SpecialityModel> allSpecialityModelList = super.list(null);

        //构建专业模型树并返回
        return getChildren(0L,allSpecialityModelList);
    }


    /**
     * 查询专业模型子节点集合
     */
    public List<SpecialityModel> getChildren(Long parentId,List<SpecialityModel> specialityModelList){

        //查询指定的节点集合中的子节点列表
        List<SpecialityModel> children = specialityModelList.stream().filter(c -> Objects.equals(c.getParentId(), parentId)).collect(Collectors.toList());

        //判断子节点列表是否为空
        if (ObjectUtils.isEmpty(children)){
            //若为空则直接返回空的集合
            return new ArrayList<>();
        }

        //否则遍历子节点列表
        children.forEach(child -> {

            //获取子节点的id
            Long pId = child.getId();

            //递归查询子节点对应的 子节点列表
            List<SpecialityModel> cChildren = getChildren(pId, specialityModelList);

            //将子节点列表添加到 子节点中
            child.getChildren().addAll(cChildren);
        });

        //返回子节点列表
        return children;
    }
}
